package com.cskaoyan.service.state;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.po.stat.GoodsInfo;
import com.cskaoyan.bean.po.stat.OrderInfo;
import com.cskaoyan.bean.vo.stat.StatVo;
import com.cskaoyan.bean.po.stat.UserInfo;
import com.cskaoyan.mapper.stat.StatGoodsMapper;
import com.cskaoyan.mapper.stat.StatOrderMapper;
import com.cskaoyan.mapper.stat.StatUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: market
 * @description: 统计服务实现类注册到容器
 * @author: zhong
 * @create: 2022-07-18 16:33
 **/
@Service
public class StatServiceImpl implements StatService {
    @Autowired
    StatUserMapper statUserMapper;
    @Autowired
    StatOrderMapper statOrderMapper;
    @Autowired
    StatGoodsMapper statGoodsMapper;

    private List<String> getColumns(Class clazz) {
        ArrayList columns = new ArrayList();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            columns.add(field.getName());
        }
        return columns;
    }

    @Override
    public StatVo statOrderOp() {
        StatVo statVo = new StatVo();
        //列：横纵坐标
        List<String> columns = getColumns(OrderInfo.class);
        statVo.setColumns(columns);

        /**
         * sql:
         * select  date_format(add_time,'%Y-%m-%d') as `day`,
         *        count(`id`) as `orders`,
         *        count(`user_id`) as `customers`,
         *        sum(`order_price`) as `amount`,
         *         (sum(`order_price`)/count(`user_id`)) as `pcr`
         * from market_order
         * group by day;
         */
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.select("date_format(add_time,'%Y-%m-%d') as `day`,count(`id`) as `orders`,count(`user_id`) as `customers`,sum(`order_price`) as `amount`").groupBy("day");
        List<OrderInfo> infos = statOrderMapper.selectList(wrapper);
        for (OrderInfo info : infos) {
            double amount = info.getAmount().doubleValue();
            info.setPcr(BigDecimal.valueOf(amount/info.getCustomers()));
        }
        statVo.setRows(infos);
        return statVo;
    }

    @Override
    public StatVo statUserOp() {
        StatVo statVo = new StatVo();
        //列：横纵坐标
        List<String> columns = getColumns(UserInfo.class);
        statVo.setColumns(columns);

        //行：数据信息值
        /**
         *select  date_format(add_time,'%Y-%m-%d') as day,
         *        count(`id`) as `users`
         * from market_user
         * group by day;
         */
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.select("date_format(add_time,'%Y-%m-%d') as day, count(`id`) as `users`").groupBy("day");
        List<UserInfo> infos = statUserMapper.selectList(wrapper);
        statVo.setRows(infos);
        return statVo;
    }

    @Override
    public StatVo statGoodsOp() {
        StatVo statVo = new StatVo();
        //列：横纵坐标
        List<String> columns = getColumns(GoodsInfo.class);
        statVo.setColumns(columns);

        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.select("date_format(add_time,'%Y-%m-%d') as `day`, count(`order_id`) as `orders`,sum(`number`) as `products`,sum(`price`*`number`) as `amount` ").groupBy("day");
        List<GoodsInfo> infos = statGoodsMapper.selectList(wrapper);
        statVo.setRows(infos);
        return statVo;
    }
}
